package com.ssms.controller;

import com.ssms.commons.Result;
import com.ssms.commons.TablePage;
import com.ssms.model.SysUser;
import com.ssms.model.XjApply;
import com.ssms.service.XjApplyService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("xjApply")
public class XjApplyController {

    @Resource
    private XjApplyService xjApplyService;

    @RequestMapping()
    public String xjApply() {
        return "xjApply";
    }

    @RequestMapping("myXjApply")
    public String myXjApply() {
        return "myXjApply";
    }

    @RequestMapping("list")
    @ResponseBody
    public TablePage list(TablePage tablePage,
                          @RequestParam(value = "searchValue",required = false) String searchValue,
                          @RequestParam(value = "stuId",required = false) Integer stuId,
                          HttpSession session) {
        SysUser fdy = (SysUser) session.getAttribute("sysUser");
        Integer fdyId= null;
        if(fdy == null){
            return tablePage;
        }

        if(fdy.getRoleType() == 2){
            fdyId = fdy.getId();
        }
        return xjApplyService.search(tablePage, searchValue,fdyId,stuId);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(XjApply xjApply) {
        return xjApplyService.update(xjApply);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Integer id) {
        return xjApplyService.delete(id);
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(XjApply xjApply,HttpSession session) {
        SysUser stu = (SysUser) session.getAttribute("sysUser");
        xjApply.setStuId(stu.getId());
        xjApply.setApplyDate(new Date());
        return xjApplyService.save(xjApply);
    }
}
